// pages/reservation/reservation.js
var common = require('../../js/common.js');
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    date: '2016-09-01',
    yuyuelist: [],
    yysxlist: [],
    hidden: true,
    quanxian: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '我的工作',

    })

    this.setData({
      yjyz: wx.getStorageSync('userInfo').type
    })
    var userInfo = wx.getStorageSync('userInfo');
    //没有openid

    if (!userInfo) {
      wx.redirectTo({
        url: '/pages/index/index'
      })
      return;
    }

    //获取当前时间戳
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log("当前时间戳为：" + timestamp);

    //获取当前时间
    var n = timestamp * 1000;
    var date = new Date(n);
    //年
    var Y = date.getFullYear();
    //月
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //日
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    // //时
    // var h = date.getHours();
    // //分
    // var m = date.getMinutes();
    // //秒
    // var s = date.getSeconds();
    // console.log("当前时间：" + Y +'-'+ M+'-'+ D);
    var dqtime = '';
    console.log(options.time)
    if (options.time) {
      this.setData({
        date: options.time
      })
      dqtime = options.time;
    } else {
      this.setData({
        date: Y + '-' + M + '-' + D
      })
      dqtime = Y + '-' + M + '-' + D;
    }
    var that = this;

    //获取积分
    common.request(app.globalData.localUrl, 'authuser', {
        yiyuan_id: wx.getStorageSync('yiyuan_id'),
        user_id: wx.getStorageSync('userInfo').id
      },
      function(res) {
        // console.log(res.data.data.gsy_status)

        //获取积分
        var data66 = {};
        if (wx.getStorageSync('userInfo').type == 1) {
          console.log('我是院长')
          data66 = {
            yiyuan_id: wx.getStorageSync('yiyuan_id'),
            yuyuetime: dqtime
          };
        } else {
          if (res.data[14]) {
            data66 = {
              yiyuan_id: wx.getStorageSync('yiyuan_id'),
              yuyuetime: dqtime
            };
          } else {
            data66 = {
              user_id: wx.getStorageSync('userInfo').id,
              yiyuan_id: wx.getStorageSync('yiyuan_id'),
              yuyuetime: dqtime
            };
          }
        
        }
        common.request(app.globalData.localUrl, 'yuyuelist', data66,
          function (res) {
            console.log('预约列表11111', res)
            if (res.data.code == 1) {
              // console.log(res.data.data.gsy_status)
              that.setData({
                yuyuelist: res.data.data
              })
            }
          }, 'rtype= post');
      }, 'rtype= post');



    //预约事项
    common.request(app.globalData.localUrl, 'typelist', {},
      function(res) {
        console.log('预约事项列表', res)
        if (res.data.code == 1) {
          that.setData({
            yysxlist: res.data.data
          })
        }
      }, 'rtype= post');

  },
  //查看邀请码
  yaoqingma: function() {
    wx.navigateTo({
      url: '/pages/yaoqingma/yaoqingma',
    })
  },

  //查看详情
  yyxq: function(e) {
    console.log('id=' + e.target.dataset.id)
    var id = e.target.dataset.id;
    wx.navigateTo({
      url: '/pages/yyxq/yyxq?id=' + id,
    })
  },
  //编辑权限
  quanxian: function() {
    wx.navigateTo({
      url: '/pages/qxlist/qxlist?id=' + wx.getStorageSync('yiyuan_id'),
    })
  },
  //数据统计
  tongji: function() {
    wx.navigateTo({
      url: '/pages/tongji/tongji?id=' + wx.getStorageSync('yiyuan_id'),
    })
  },
  //添加预约
  addyy: function() {
    wx.navigateTo({
      url: '/pages/addyy/addyy',
    })
  },
  //跳转诊所列表
  zhuye: function() {
    wx.switchTab({
      url: '/pages/cliniclist/cliniclist',
    })
  },

  //跳转到处邮箱
  youxiang: function() {
    wx.navigateTo({
      url: '/pages/email/email',
    })
  },


  //我的员工
  yuangong: function() {
    wx.navigateTo({
      url: '/pages/employees/employees?id=' + wx.getStorageSync('yiyuan_id'),
    })
  },
  //删除

  shanchu: function(e) {
    var id = e.target.dataset.id;
    this.setData({
      hidden: false,
      delid: id
    });
  },
  cancel: function() {
    this.setData({
      hidden: true
    });
  },
  confirm: function() {
    this.setData({
      hidden: true
    });

    var that = this;
    //获取积分
    common.request(app.globalData.localUrl, 'deljiuzhen', {
        id: that.data.delid
      },
      function(res) {
        console.log('删除预约', res)
        var e = {};
        e.time = that.data.date;
        if (res.data.code == 1) {
          that.onLoad(e);
        }
      }, 'rtype= post');

  },

  //日期选择器：
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })

    var that = this;
    //获取积分
    common.request(app.globalData.localUrl, 'yuyuelist', {
        yiyuan_id: wx.getStorageSync('yiyuan_id'),
        yuyuetime: e.detail.value,
      user_id: wx.getStorageSync('userInfo').id
      },
      function(res) {
        console.log('预约列表666', res)
        if (res.data.code == 1) {
          // console.log(res.data.data.gsy_status)
          that.setData({
            yuyuelist: res.data.data
          })
        }
      }, 'rtype= post');


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取积分
    common.request(app.globalData.localUrl, 'authuser', {
        yiyuan_id: wx.getStorageSync('yiyuan_id'),
        user_id: wx.getStorageSync('userInfo').id
      },
      function(res) {
        console.log('权限列表', res)
        // console.log(res.data.data.gsy_status)
        that.setData({
          quanxian: res.data
        })
      }, 'rtype= post');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})